package com.atchangsha.system.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.atchangsha.system.constant.MenusConstants;
import com.atchangsha.system.dto.MenusDTO;
import com.atchangsha.system.vo.MenusVO;
import org.springframework.web.bind.annotation.*;
import com.atchangsha.system.entity.MenusEntity;
import com.atchangsha.system.service.MenusService;
import com.atchangsha.system.common.utils.PageUtils;
import com.atchangsha.system.common.utils.Result;

import javax.annotation.Resource;


/**
 * 菜单表
 *
 * @author 卓越班项目第二组
 * @date 2022-04-22 00:49:58
 */
@RestController
@RequestMapping("system/menus")
@CrossOrigin
public class MenusController {
    @Resource
    private MenusService menusService;

    /**
     * 查询出所有菜品
     * @return
     */
    @GetMapping("/listAll")
    public Result listAll(){
        List<MenusEntity> list = menusService.list();
        List<MenusVO> menusVOList = list.stream().map(item -> {
            MenusVO menusVO = item.toVO();
            menusVO.setNumber(1);
            return menusVO;
        }).collect(Collectors.toList());
        return Result.ok().setResult(menusVOList);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageUtils page = menusService.queryPage(params);
        return Result.ok(MenusConstants.MenusEnum.MENUS_LIST_SUCCESS).setResult(page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id) {
        MenusEntity menus = menusService.getById(id);
        //判断所查询的菜单是否为空
        if (menus == null) {
            return Result.fail(MenusConstants.MenusEnum.MENUS_INFO_FILE);
        }
        return Result.ok(MenusConstants.MenusEnum.MENUS_INFO_SUCCESS).setResult(menus);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody MenusDTO menus) {
        //判断所插入的菜品是否存在
        MenusEntity menusEntity = menus.toEntity();
        MenusEntity menusExit = menusService.getById(menusEntity.getId());
        if (menusExit != null) {
            return Result.fail(MenusConstants.MenusEnum.MENUS_SAVE_FILE);
        }
        menusService.save(menusEntity);
        return Result.ok(MenusConstants.MenusEnum.MENUS_SAVE_SUCCESS).setResult(menusEntity);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody MenusDTO menus) {
        if (menus == null) {
            return Result.fail(MenusConstants.MenusEnum.MENUS_UPDATE_FAIL);
        }
        //将DTO转换成entity
        MenusEntity menusEntity = menus.toEntity();

        //修改操作
        menusService.updateById(menusEntity);
        return Result.ok(MenusConstants.MenusEnum.MENUS_UPDATE_SUCCESS);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Long[] ids) {
        boolean flag = menusService.removeByIds(Arrays.asList(ids));
        if(!flag){
            return Result.fail(MenusConstants.MenusEnum.MENUS_DELETE_FAIL);
        }
        return Result.ok(MenusConstants.MenusEnum.MENUS_DELETE_SUCCESS);
    }
}
